package GUILayer;

import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.Font;
import java.util.Calendar;
import java.sql.Date;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import java.awt.Point;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JSeparator;

public class CalendarPanel extends JPanel 
{
	private Calendar cal;
	private Calendar dateToday;
	private JLabel selectedDay;
	
	private JLabel lblMonthYear;
	private JPanel daysOfMonth;
	
	private JButton btnPrev;
	private JButton btnNext;
	
	public CalendarPanel()
	{	
		setBorder(new LineBorder(Color.GRAY));
		
		setSize(new Dimension(225, 185));
		setLayout(null);
		
		cal = Calendar.getInstance();
		
		dateToday = Calendar.getInstance();
		dateToday.setTime(cal.getTime());
		
		initComponents();
		
		updateCalendar();
		
		selectedDay = (JLabel)daysOfMonth.getComponent(0);
		
		selectedDay.setBorder(new LineBorder(Color.GRAY));
	}
	
	private void updateCalendar()
	{
		lblMonthYear.setText(getMonth(cal.get(Calendar.MONTH)) + " " + cal.get(Calendar.YEAR));
		
		int startDay = 0;
		
		btnPrev.setEnabled(true);
		
		if(cal.compareTo(dateToday) <= 0)
		{
			startDay = cal.get(Calendar.DAY_OF_MONTH) - 1;
			
			btnPrev.setEnabled(false);
		}
		
		addMonthDays(startDay);
	}
	
	private String getMonth(int month)
	{
		String str = "";
		
		switch(month)
		{
			case 0: str = "Januar";
			break;
			case 1: str = "Februar";
			break;
			case 2: str = "Marts";
			break;
			case 3: str = "April";
			break;
			case 4: str = "Maj";
			break;
			case 5: str = "Juni";
			break;
			case 6: str = "Juli";
			break;
			case 7: str = "August";
			break;
			case 8: str = "September";
			break;
			case 9: str = "Oktober";
			break;
			case 10: str = "November";
			break;
			case 11: str = "December";
			break;
		}
		
		return str;
	}
	
	private void increaseMonth()
	{
		cal.add(Calendar.MONTH, 1);
		
		selectedDay = null;
		
		updateCalendar();
	}
	
	private void decreaseMonth()
	{
		cal.add(Calendar.MONTH, -1);
		
		selectedDay = null;
		
		updateCalendar();
	}
	
	private void chooseDay(MouseEvent args)
	{
		Point point = args.getPoint();
		
		JLabel comp = (JLabel)daysOfMonth.getComponentAt(point);
		
		if(comp != null)
		{
			if(selectedDay != null)
			{
				selectedDay.setBorder(null);
			}
			
			selectedDay = comp;
			
			selectedDay.setBorder(new LineBorder(Color.GRAY));
			
			BookingPanel panel = (BookingPanel)this.getParent();
			panel.updateDateInfo();
		}
	}
	
	private void addMonthDays(int startIndex)
	{
		daysOfMonth.removeAll();
		
		int maxDays = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		
		for(int i = startIndex; i < maxDays; i++)
		{
			JLabel lblDay = new JLabel();
			lblDay.setFont(new Font("Tahoma", Font.PLAIN, 12));
			lblDay.setText(String.valueOf(i + 1));
			//lblDay.setBounds(10, 3, 5, 5);
			
			daysOfMonth.add(lblDay);
		}
		
		daysOfMonth.repaint();
	}
	
	public Date getSelectedDate()
	{
		Date date = null;
		
		if(selectedDay != null)
		{
			int day = Integer.valueOf(selectedDay.getText());
			
			cal.set(Calendar.DAY_OF_MONTH, day);
			
			date = new Date(cal.getTimeInMillis());
		}
		
		return date;
	}
	
	private void initComponents()
	{
		lblMonthYear = new JLabel();
		lblMonthYear.setBounds(10, 23, 108, 14);
		lblMonthYear.setFont(new Font("Tahoma", Font.PLAIN, 12));
		add(lblMonthYear);
		
		btnPrev = new JButton("<");
		btnPrev.setBounds(128, 16, 41, 23);
		btnPrev.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				decreaseMonth();
			}
		});
		add(btnPrev);
		
		btnNext = new JButton(">");
		btnNext.setBounds(174, 16, 41, 23);
		btnNext.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				increaseMonth();
			}
		});
		add(btnNext);
		
		daysOfMonth = new JPanel();
		daysOfMonth.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				chooseDay(arg0);
			}
		});
		daysOfMonth.setBounds(10, 57, 205, 117);
		daysOfMonth.setBackground(Color.WHITE);
		daysOfMonth.setBorder(new LineBorder(Color.GRAY));
		daysOfMonth.setLayout(new GridLayout(7, 5));
		add(daysOfMonth);
		
		JSeparator separator = new JSeparator();
		separator.setBounds(10, 47, 205, 2);
		add(separator);
	}
}
